package com.zhixue.content.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zhixue.content.feignclient.SearchServiceClient;
import com.zhixue.content.feignclient.po.CourseIndex;
import com.zhixue.content.model.po.CoursePublish;
import com.zhixue.content.service.CoursePublishService;
import com.zhixue.message.model.po.MqMessage;
import com.zhixue.message.service.MqMessageService;
import com.zhixue.message.service.impl.MessageProcessAbstract;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Auther: 李 力
 * @Date: 2025/5/7
 * @Description: com.zhixue.content.job
 * @version: 1.0
 */
@Slf4j
@Component
public class CoursePublishTask extends MessageProcessAbstract {

    @Autowired
    private CoursePublishService coursePublishService;

    @Autowired
    private SearchServiceClient searchServiceClient;

    //任务调度入口
    @XxlJob("CoursePublishJobHandler")
    public void coursePublishJobHandler() throws Exception {
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        super.process(shardIndex, shardTotal, "course_publish", 30, 60);
    }

    //执行课程发布任务的逻辑
    @Override
    public boolean execute(MqMessage mqMessage) {
        if (mqMessage == null) {
            return false;
        }
        String businessKey1 = mqMessage.getBusinessKey1();
        Long courseId = Long.parseLong(businessKey1);
        //页面静态化，上传到minio
        generateCourseHtml(mqMessage, courseId);
        //保存课程索引信息
        saveCourseIndex(mqMessage, courseId);
        //将课程信息缓存至redis
        saveCourseCache(mqMessage, courseId);
        return true;
    }

    //页面静态化，上传到minio
    public void generateCourseHtml(MqMessage mqMessage, long courseId) {
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int stageOne = mqMessageService.getStageOne(taskId);
        if (stageOne > 0) {
            log.debug("课程静态化任务完成");
            return;
        }
        //进行课程静态化
        //生成html
//        File file = coursePublishService.generateCourseHtml(courseId);
//        if (file == null) {
//            log.debug("生成html失败");
//            return;
//        }
//        //上传html文件到minio
//        coursePublishService.uploadCourseHtml(courseId, file);
        //任务处理完成
        mqMessageService.completedStageOne(taskId);
    }

    //将课程信息缓存至redis
    public void saveCourseCache(MqMessage mqMessage, long courseId) {
        log.debug("将课程信息缓存至redis,课程id:{}", courseId);
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int three = mqMessageService.getStageThree(taskId);
        if (three > 0) {
            log.debug("课程信息缓存至redis完成");
            return;
        }
        try {
            //todo 将课程信息缓存至redis 后续完成

            mqMessageService.completedStageThree(taskId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //保存课程索引信息
    public void saveCourseIndex(MqMessage mqMessage, long courseId) {
        log.debug("保存课程索引信息,课程id:{}", courseId);
        Long taskId = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int stageTwo = mqMessageService.getStageTwo(taskId);
        if (stageTwo > 0) {
            log.debug("保存课程索引信息完成");
            return;
        }
        try {
            //查询课程
            CoursePublish coursePublish = coursePublishService.getById(courseId);
            //向es写数据
            CourseIndex courseIndex = new CourseIndex();
            BeanUtils.copyProperties(coursePublish, courseIndex);
            Boolean result = searchServiceClient.add(courseIndex);
            if (!result) {
                throw new RuntimeException("保存课程索引信息失败");
            }
            mqMessageService.completedStageTwo(taskId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
